// 定义一个自定义的网络请求工具类
const baseURL = 'http://localhost:8888'
const http = {
  // GET请求
  get(url, data={}, cfg={}) {
    return new Promise((resolve, reject) => {
      uni.request({
		  url,
        // url: `${cfg.baseURL || baseURL}${url}`,
		data,
        method: 'GET',
		header: {'Content-Type': 'application/json', ...cfg, Authorization: uni.getStorageSync('Authorization')},
        success: res => {
			if(res.statusCode === 200) {
				resolve({ok: true, msg: '请求成功', data: res.data.data})
			} else {
				reject({data: null, msg: '系统异常', ok: false})
			}
		},
        fail: err => reject(err)
      })
    });
  },
  
  // POST请求
  post(url, data, cfg={}) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: `${cfg.baseURL || baseURL}${url}`,
        method: 'POST',
        data,
        header: {'Content-Type': 'application/json', ...cfg, Authorization: uni.getStorageSync('Authorization')},
        success: res => {
			if(res.statusCode === 200) {
				resolve({ok: true, msg: '请求成功', data: res.data.data})
			} else {
				reject({data: null, msg: '系统异常', code: 500})
			}
		},
        fail: err => reject(err)
      })
    });
  }
}
 
export default http;